package com.ruoyi.web.controller.blog;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.BizType;
import com.ruoyi.blog.service.IBizTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 文章类型Controller
 * 
 * @author wsa
 * @date 2019-10-19
 */
@Controller
@RequestMapping("/blog/type")
public class BizTypeController extends BaseController
{
    private String prefix = "blog/type";

    @Autowired
    private IBizTypeService bizTypeService;

    @RequiresPermissions("blog:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询文章类型树列表
     */
    @RequiresPermissions("blog:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BizType> list(BizType bizType)
    {
        List<BizType> list = bizTypeService.selectBizTypeList(bizType);
        return list;
    }

    /**
     * 导出文章类型列表
     */
    @RequiresPermissions("blog:type:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizType bizType)
    {
        List<BizType> list = bizTypeService.selectBizTypeList(bizType);
        ExcelUtil<BizType> util = new ExcelUtil<BizType>(BizType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增文章类型
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bizType", bizTypeService.selectBizTypeById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存文章类型
     */
    @RequiresPermissions("blog:type:add")
    @Log(title = "文章类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizType bizType)
    {
    	String loginName = ShiroUtils.getLoginName();
    	bizType.setCreateBy(loginName);
    	bizType.setUpdateBy(loginName);
        return toAjax(bizTypeService.insertBizType(bizType));
    }

    /**
     * 修改文章类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizType bizType = bizTypeService.selectBizTypeById(id);
        mmap.put("bizType", bizType);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章类型
     */
    @RequiresPermissions("blog:type:edit")
    @Log(title = "文章类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizType bizType)
    {
    	String loginName = ShiroUtils.getLoginName();
    	bizType.setUpdateBy(loginName);
        return toAjax(bizTypeService.updateBizType(bizType));
    }

    /**
     * 删除
     */
    @RequiresPermissions("blog:type:remove")
    @Log(title = "文章类型", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(bizTypeService.deleteBizTypeById(id));
    }

    /**
     * 选择文章类型树
     */
    @GetMapping(value = { "/selectTypeTree/{id}", "/selectTypeTree/" })
    public String selectTypeTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bizType", bizTypeService.selectBizTypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载文章类型树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = bizTypeService.selectBizTypeTree();
        return ztrees;
    }
}
